package com.zhou.project.configurations.mybatis;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.session.ExecutorType;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.aop.interceptor.PerformanceMonitorInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * @project spring-boot-project
 * @author zhouhonggang
 * @datetime 2021/8/14 16:32
 * @version 1.0.0
 * @description: Mybatis自定义配置信息
 */
@Configuration
@MapperScans({
    @MapperScan(basePackages = "com.zhou.project.modules.*.dao"),
    @MapperScan(basePackages = "com.zhou.project.modules.*.*.dao")
})
public class MybatisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //注册分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    /**
     * 指定默认执行器
     * @return SIMPLE
     */
    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setDefaultExecutorType(ExecutorType.SIMPLE);
    }

    /**
     * SQL执行效率插件, 性能分析拦截器，用于输出每条 SQL 语句及其执行时间
     * @return PerformanceMonitorInterceptor
     */
    @Bean
    @Profile({"dev", "pro"})
    public PerformanceMonitorInterceptor performanceInterceptor() {
        return new PerformanceMonitorInterceptor();
    }

}
